package com.example.libretranslatemobile.settings.connection;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.libretranslatemobile.R;
import com.google.android.material.snackbar.Snackbar;

public class ConnectionActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton officialHostRB, customHostRB;
    EditText customHostEditText, officialHostEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connection);
        setTitle("Connections");
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        init();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb = group.findViewById(checkedId);
            if(rb.getId() == officialHostRB.getId()){
                customHostEditText.setVisibility(View.GONE);
                Snackbar
                        .make(group.getRootView(), "Give API key?", Snackbar.LENGTH_LONG)
                        .setAction("GIVE", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent browserIntent = new
                                        Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/LibreTranslate/LibreTranslate#mirrors"));
                                startActivity(browserIntent);
                            }
                        })
                        .show();

                officialHostEditText.setVisibility(View.VISIBLE);
                //Toast.makeText(context, "Clicked id: "+checkedId+"\noffocialID "+officialHost.getId()+"\nCustomId "+customHost.getId() , Toast.LENGTH_SHORT).show();
            }else if(rb.getId() == customHostRB.getId()){
                officialHostEditText.setVisibility(View.GONE);

                customHostEditText.setVisibility(View.VISIBLE);
            }
            }
        });
    }

    private void editTextActsInRG(EditText editText){
        editText.setHeight(0);
    }

    private void init(){
        radioGroup = findViewById(R.id.connections_rg);
        officialHostRB = findViewById(R.id.official_host_rb);
        customHostRB = findViewById(R.id.custom_host_rb);
        customHostEditText = findViewById(R.id.custom_host_edit_text);
        officialHostEditText = findViewById(R.id.official_host_edit_text);
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}